package day05.concurrent02;

import java.util.concurrent.Exchanger;

/**
 * 5.Exchanger 交换机
 */
public class Demo02 {
    public static void main(String[] args) {
        Exchanger<String> exchanger = new Exchanger<>();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String s = exchanger.exchange("天王盖地虎！");
                    System.out.println("第一个"+s);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String s = exchanger.exchange("宝塔镇河妖！");
                    System.out.println("第二个"+s);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
